home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / hw.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.6 KB  |  148 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import re
  21. import logging
  22.  
  23. from checkbox.lib.cache import cache
  24.  
  25. from checkbox.properties import String
  26. from checkbox.registries.command import CommandRegistry
  27. from checkbox.registries.data import DataRegistry
  28. from checkbox.registries.map import MapRegistry
  29.  
  30.  
  31. class DeviceRegistry(DataRegistry):
  32.     """Registry for HW device information.
  33.  
  34.     Each item contained in this registry consists of the properties of
  35.     the corresponding HW device.
  36.     """
  37.  
  38.     @cache
  39.     def items(self):
  40.         items = []
  41.         lines = []
  42.  
  43.         id = status = depth = None
  44.         for line in self.split("\n"):
  45.             if not line:
  46.                 continue
  47.  
  48.             match = re.match(r"(\s+(\*-)?)(.+)", line)
  49.             if not match:
  50.                 raise Exception, "Invalid line: %s" % line
  51.  
  52.             space = len(match.group(1))
  53.             if depth is None:
  54.                 depth = space
  55.  
  56.             if space > depth:
  57.                 lines.append(line)
  58.             elif match.group(2) is not None:
  59.                 if id is not None:
  60.                     value = DeviceRegistry("\n".join(lines))
  61.                     lines = []
  62.  
  63.                     items.append((id, value))
  64.                     items.append(("status", status))
  65.  
  66.                 node = match.group(3)
  67.                 match = re.match(r"([^\s]+)( [A-Z]+)?", node)
  68.                 if not match:
  69.                     raise Exception, "Invalid node: %s" % node
  70.  
  71.                 id = match.group(1)
  72.                 status = match.group(2)
  73.             else:
  74.                 (key, value) = match.group(3).split(": ", 1)
  75.                 key = key.replace(" ", "_")
  76.  
  77.                 # Parse potential list or dict values
  78.                 values = value.split(" ")
  79.                 if key == "product":
  80.                     match = re.search(r"(.*) \[[0-9A-F]{1,4}:([0-9A-F]{1,4})\]$",
  81.                         value)
  82.                     if match:
  83.                         value = match.group(1)
  84.                         items.append(("product_id", int(match.group(2), 16)))
  85.                 elif key == "vendor":
  86.                     match = re.search(r"(.*) \[([0-9A-F]{1,4})\]$", value)
  87.                     if match:
  88.                         value = match.group(1)
  89.                         items.append(("vendor_id", int(match.group(2), 16)))
  90.                 elif key.endswith("s"):
  91.                     value = values
  92.                 elif [v for v in values if "=" in v]:
  93.                     index = 1
  94.                     for value in values[1:]:
  95.                         if "=" not in value:
  96.                             values[index - 1] += " %s" % values.pop(index)
  97.                         else:
  98.                             index += 1
  99.                     value = dict((v.split("=", 1) for v in values))
  100.                     value = MapRegistry(value)
  101.  
  102.                 items.append((key, value))
  103.  
  104.         if lines:
  105.             value = DeviceRegistry("\n".join(lines))
  106.             items.append((id, value))
  107.  
  108.         return items
  109.  
  110.  
  111. class HwRegistry(CommandRegistry):
  112.     """Registry for HW information.
  113.  
  114.     Each item contained in this registry consists of the hardware id as
  115.     key and the corresponding device registry as value.
  116.     """
  117.  
  118.     # Command to retrieve hw information.
  119.     command = String(default="lshw -numeric 2>/dev/null")
  120.  
  121.     # Command to retrieve the hw version.
  122.     version = String(default="lshw -version 2>/dev/null")
  123.  
  124.     @cache
  125.     def __str__(self):
  126.         logging.info("Running command: %s", self.version)
  127.         version = os.popen(self.version).read().strip()
  128.         numbers = version.split(".")
  129.         if len(numbers) == 3 \
  130.            and numbers[0] == "B" \
  131.            and int(numbers[1]) == 2 \
  132.            and int(numbers[2]) < 13:
  133.             self.command = self.command.replace(" -numeric", "")
  134.  
  135.         return super(HwRegistry, self).__str__()
  136.  
  137.     @cache
  138.     def items(self):
  139.         lines = self.split("\n")
  140.  
  141.         key = lines.pop(0)
  142.         value = DeviceRegistry("\n".join(lines))
  143.  
  144.         return [(key, value)]
  145.  
  146.  
  147. factory = HwRegistry
  148.